//
//  ThumbnailCollectionViewCell.swift
//  PDFSign
//
//  Created by 李永彬 on 2025/9/22.
//

import UIKit

class WJPDFThumbnailCell: UICollectionViewCell {
    
    private let imageView = UIImageView()
    private let pageLabel = UILabel()
    private let selectionBorder = UIView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        // 图片视图
        imageView.contentMode = .scaleAspectFill
        imageView.layer.cornerRadius = 4
        imageView.layer.masksToBounds = true
        imageView.backgroundColor = UIColor.clear
        contentView.addSubview(imageView)
        
        imageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        // 页码标签
        pageLabel.font = UIFont.systemFont(ofSize: 10, weight: .medium)
        pageLabel.textColor = .white
        pageLabel.textAlignment = .center
        pageLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
        pageLabel.layer.cornerRadius = 8
        pageLabel.layer.masksToBounds = true
        pageLabel.isHidden = true
        contentView.addSubview(pageLabel)
        
        pageLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(4)
            make.trailing.equalToSuperview().offset(-4)
            make.width.height.equalTo(16)
        }
        
        // 选中边框
        selectionBorder.layer.borderWidth = 2
        selectionBorder.layer.borderColor = UIColor(hex: "#1F87F6").cgColor
        selectionBorder.layer.cornerRadius = 4
        selectionBorder.isHidden = true
        contentView.addSubview(selectionBorder)
        
        selectionBorder.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(-2)
        }
    }
    
    func configure(with item: WJPDFContentViewController.PDFThumbnailItem) {
        pageLabel.text = "\(item.pageIndex + 1)"
        imageView.image = item.thumbnailImage
        
        // 更新选中状态
        selectionBorder.isHidden = !item.isSelected
//        contentView.alpha = item.isSelected ? 1.0 : 0.7
        
//        // 选中状态缩放动画
//        UIView.animate(withDuration: 0.2) {
//            self.transform = item.isSelected ? CGAffineTransform(scaleX: 1.1, y: 1.1) : .identity
//        }
    }
    
    func animateSelection() {
        // 点击动画
//        UIView.animate(withDuration: 0.1) {
//            self.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
//        }
//        UIView.animate(withDuration: 0.1, animations: {
//            self.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
//        }) { _ in
//            UIView.animate(withDuration: 0.1) {
//                self.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
//            }
//        }
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        imageView.image = nil
        selectionBorder.isHidden = true
        transform = .identity
        contentView.alpha = 1.0
    }
}
